﻿const fs = require('fs');
const path = require('path');
const promisify = require('util').promisify;
const RawSource = require('webpack-sources').RawSource;
const readFile = promisify(fs.readFile);

/**
 * 一个用于构建示例文件的webpack 插件
 * @param {Object} config Plugin configuration.  Requires a `templates` property
 * with the path to templates and a `common` property with the name of the
 * common chunk.
 * @constructor
 */
function ExampleBuilder(config) {
    this.templates = config.templates;
    this.common = config.common;
    this.htmls = config.htmls;
}

/**
 * 由 webpack调用
 * @param {Object} compiler  webpack compiler.
 */
ExampleBuilder.prototype.apply = function (compiler) {
    compiler.hooks.emit.tapPromise('ExampleBuilder', async (compilation) => {
        const exampleConfig = {};
        console.log();
        for (let name in this.htmls) {

            var nameSplit = name.split("_");

            var tree1 = nameSplit[0];
            var tree2 = nameSplit[1];
            var tree3 = nameSplit[2];

            if (exampleConfig[tree1] == undefined) {
                exampleConfig[tree1] = {};
                exampleConfig[tree1].name = tree1;
                exampleConfig[tree1].name_en = tree1;
                exampleConfig[tree1].content = {};
            }

            if (exampleConfig[tree1].content[tree2] == undefined) {
                exampleConfig[tree1].content[tree2] = {};
                exampleConfig[tree1].content[tree2].name = tree2;
                exampleConfig[tree1].content[tree2].name_en = tree2;
                exampleConfig[tree1].content[tree2].content = [];
            }

            exampleConfig[tree1].content[tree2].content.push({
                name: tree3,
                name_en: tree3,
                thumbnail: name + ".jpg",
                fileName: name,
                page: name,
            });


            //for (const file in assets) {
            const readOptions = { encoding: 'utf8' };

            const htmlName = `${name}.html`;
            const htmlPath = path.join(compiler.context, htmlName);
            console.log(htmlPath);
            //html源码
            const htmlSource = await readFile(htmlPath, readOptions);
            compilation.assets[htmlName] = new RawSource(htmlSource);
        }
        //生成config配置文件
        var configSource = 'var identification = {name: "Cesium Demo"};';

        configSource += `var exampleConfig = ${JSON.stringify(exampleConfig)};`;

        configSource += 'var sideBarIconConfig = {"baseDemo": "fa-edit","map": "fa-map-marker","overlay": "fa-image","control": "fa-cog","popup": "fa-commenting-o","query": "fa-search","theme": " fa-area-chart","analysis": "fa-leanpub","viz": "fa-map", "OGC": "fa fa-globe","others": "fa-th-large"}; ';

        configSource += 'var exampleIconConfig = {"baseDemo": "fa-edit","map": "fa-map-marker", "overlay": "fa-image","control": "fa-cog","popup": "fa-commenting-o","query": "fa-search","theme": " fa-area-chart","analysis": "fa-leanpub","viz": "fa-map","OGC": "fa fa-globe","others": "fa-th-large"}; ';
        compilation.assets['js/config.js'] = new RawSource(configSource);
    });
};


module.exports = ExampleBuilder;
